﻿using Jackletter.BlendAuth;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace TestWeb.Controllers
{
    /// <summary>
    /// 混合式身份认证
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthController : ControllerBase
    {


        public AuthController()
        {
        }

        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">用户密码</param>
        /// <param name="day">有效期(天)</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login([FromBody] LoginDto loginDto)
        {
            if (loginDto == null) return Ok(new
            {
                Success = false,
                Data = "必须输入用户名密码!"
            });
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                return Ok(new
                {
                    Success = false,
                    Data = "不允许重复登录!"
                });
            }
            if (loginDto.UserName == "admin" && loginDto.Password == "1")
            {
                string token = HttpContext.SignInBlend(1);
                return Ok(new
                {
                    Success = true,
                    Data = new
                    {
                        userInfo = new
                        {

                        },
                        token = token
                    }
                });
            }
            return Ok(new
            {
                Success = false,
                Data = "登录失败!"
            });
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public IActionResult LogOut()
        {
            HttpContext.SignOutBlend();
            return Ok(new
            {
                Success = true,
                Data = "注销成功!"
            });
        }

        /// <summary>
        /// 强制某个人退出登录
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public IActionResult ForceLogout(int userId)
        {
            HttpContext.SignOutBlend(userId);
            return Ok(new
            {
                Success = true,
                Data = $"已经将用户[{userId}]强制注销!"
            });
        }
    }

    public class LoginDto
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public int MaxDay { get; set; }
        public string SystemCode { get; set; }
        public string ClientCode { get; set; }
        public EnumLoginType LoginType { get; set; }
    }

    public enum EnumLoginType
    {
        Password
    }
}
